package android.songfinder;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class SongFinderActivity extends Activity {
	 EditText	txtbox1;
	 //EditText   txtbox2;
	 //Button     button1;
	 //TextView tv;
	 public static ArrayList<String> displayItems = new ArrayList<String>();
	 
	 private ListView lv1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	SongFinderListViewAdapter.activity = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtbox1=  (EditText) findViewById(R.id.txtbox1);
        //button1 = (Button) findViewById(R.id.button1);
        //tv = (TextView) findViewById(R.id.lbl1);
        //txtbox2=  (EditText) findViewById(R.id.txtbox2);
        //button1.setOnClickListener(new clicker());   
        
        lv1=(ListView)findViewById(R.id.ListView01);
     // By using setAdpater method in listview we an add string array in list.
        final String basePath = "/storage/Lyrics";
        File dir = new File(basePath);
        String[] children = dir.list();
        displayItems.clear();
        
        txtbox1.addTextChangedListener(new TextWatcher() {

			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				displayItems.clear();
				if (s.length() >= 2)
					findMatchingFiles(new File(basePath), displayItems, s.toString(), false);
				((BaseAdapter) lv1.getAdapter()).notifyDataSetChanged();
				
			}
			
		});

        //lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , displayItems));
        lv1.setAdapter(new SongFinderListViewAdapter(this, displayItems));
        //lv1.setOnItemClickListener(new OnItemClickListener() {
        //      public void onItemClick(AdapterView<?> parent, View view,
        //          int position, long id) {
        //    	  Uri data = Uri.parse("file://" + "/storage/Lyrics/" + displayItems.get(position)); 
        //    	  Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
        //          intent.setDataAndType(data,"text/html"); 
        //          // /Storage/Lyrics
        //          try { 
        //                    startActivity(intent); 
        //                    txtbox1.setText("");
        //             } catch (ActivityNotFoundException e) { 
        //                    e.printStackTrace(); 
        //             }            	  
        //      }
        //});
        
        //setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
        //ListView lv = getListView();
        //lv.setTextFilterEnabled(true);

        //lv.setOnItemClickListener(new OnItemClickListener() {
        //  public void onItemClick(AdapterView<?> parent, View view,
        //      int position, long id) {
            // When clicked, show a toast with the TextView text
        //    Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
        //        Toast.LENGTH_SHORT).show();
        //  }
        //});
    }
    private void findMatchingFiles(File startDir, ArrayList<String> displayItems, String srch, boolean addAll) {
    	
        File[] filesAndDirs = startDir.listFiles();
        if (filesAndDirs != null) {
            List<File> filesDirs = Arrays.asList(filesAndDirs);
            for(File file : filesDirs) {
            	if (file.isDirectory()){
            		if (file.getName().toLowerCase().contains(srch.toLowerCase()))
            			findMatchingFiles(file, displayItems, srch, true);
            		else
            			findMatchingFiles(file, displayItems, srch, false);
            	}
            	else if (addAll || file.getName().toLowerCase().contains(srch.toLowerCase()))
            		displayItems.add(file.getAbsolutePath().substring(16)); // remove /storage/Lyrics/
            }
        } else {
        	displayItems.add("http://www.google.com");
        	displayItems.add("http://www.yahoo.com");
        }
    }
    //class  clicker implements Button.OnClickListener
    //{           
    //public void onClick(View v)
    //   {
    //    String a,b;
    //    Integer vis;
    //    a =  txtbox1.getText().toString();
    //    b =  txtbox2.getText().toString();    
    //    vis =  Integer.parseInt(a)+Integer.parseInt(b);    
    //    tv.setText(vis.toString());    
    //   }
    //}
    
    private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
    
    public void SetSearchText(String txt) {
    	txtbox1.setText(txt);
    }
    
}